package com.learn.hibernatevalidator.chapter06.constraintcomposition;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * @author: liyf
 * @create: 2025-01-18 22:23
 **/

public class CheckCaseValidator implements ConstraintValidator<CheckCase, String> {

    private CaseMode caseMode;

    @Override
    public void initialize(CheckCase constraintAnnotation) {
        this.caseMode = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        boolean isValid;

        if (caseMode == CaseMode.UPPER) {
            isValid = value.toUpperCase().equals(value);
        } else {
            isValid = value.toLowerCase().equals(value);
        }


        return isValid;
    }
}